<?php

namespace app\tours\model;

use think\model\Relation;

class Tours extends Relation {

        protected $fields = ['id', 'code', 'pretitle', 'title', 'subtitle', 'uid', 'cid', 'theme_id', 'traffic', 'sight_id', 'sight_cache', 'attachpath', 'attachment', 'attachthumb', 'feature', 'content3', 'notice', 'dateline', 'travelcompany',
            'travelurl', 'supplier_id', 'hits', 'isgood', 'istop', 'ispreference', 'isindex', 'ischecked', 'isdelete', 'isnotshow', 'seo_title', 'seo_keywords', 'seo_description',
            'lead_time', 'leave_time', 'trip_days', 'price', 'traffic_id', 'labels', 'type', 'from_zone', 'img', 'tag', 'dingdan', 'manyi', 'guanzhu', 'score_jiaotong', 'score_canyin', 'score_chuyou', 'score_kefu', 'score_zhusu', 'score_daoyou'];
        protected $pk = 'id';
        protected $show_week_ch = array('日', '一', '二', '三', '四', '五', '六');
        protected $link = array(
            'tours_price' => array(
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursPrice',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id',
                'mapping_order' => "id asc"
            ),
            'tours_trip' => array(
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursTrip',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id',
                'mapping_order' => "day asc"
            ),
            'tours_case' => array(
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursCase',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id'
            )
        );

        protected function _options_filter(&$options) {
                parent::_options_filter($options);
                if (!isset($options['where']['ischecked']))
                // $options['where']['ischecked'] = 1;
                        if (!isset($options['where']['isdelete']))
                                $options['where']['isdelete'] = 0;
                return $options;
        }

        protected function _after_find(&$result, $options = array()) {
                parent::_after_find($result, $options);
                //景点
                $ld = M('LocationDetail');
                if (!empty($result['cid'])) {
                        $cids = explode(',', $result['cid']);
                }
                if (!empty($cids)) {
                        foreach ($cids as $key => $val) {
                                $result['location'][$key]['content'] = $ld->where(array('id' => $val, 'status' => '1'))->cache()->getField('content');
                        }
                }
                if (!empty($result['id'])) {
                        $result['url'] = U('/xianlu/' . $result['id'], '', 'html', false, true);
                }
                if (!empty($result['tours_trip'])) {
                        foreach ($result['tours_trip'] as $key => $value) {
                                $result['tours_trip'][$key]['images'] = array();
                                $images = unserialize($value['images']);
                                if (!empty($images)) {
                                        if (is_array($images)) {
                                                foreach ($images as $k => $val) {
                                                        $result['tours_trip'][$key]['images'][] = array('url' => "javascript:;", 'src' => $val['savepath'] . $val['savename']);
                                                }
                                        } else {
                                                $result['tours_trip'][$key]['images'][0] = array('url' => "javascript:;", 'src' => $images[0]['savepath'] . $images[0]['savename']);
                                        }
                                }

                                //行程
                                if (!empty($result['tours_trip']) && is_array($result['tours_trip'])) {
                                        $from = array("[飞机]", "[船]", "[火车]", "[汽车]");
                                        $to = array('<em style="color:#3FB7FF;">[飞机]</em>', '<em style="color:#3FB7FF;">[船]</em>', '<em style="color:#3FB7FF;">[火车]</em>', '<em style="color:#3FB7FF;">[汽车]</em>');
                                        $to1 = array('<img  alt="飞机" title="飞机" src="/public/tours/images/air.png" style="height:19px; margin-left:5px; margin-right:5px;" />', '<img alt="船" title="船" src="/public/tours/images/ship.png"  style="height:19px; margin-left:5px; margin-right:5px;"/>', '<img alt="火车" title="火车" src="/public/tours/images/train.png" style="height:19px; margin-left:5px; margin-right:5px;" />', '<img alt="汽车" title="汽车" src="/public/tours/images/vehicle.png"  style="height:19px; margin-left:5px; margin-right:5px;"/>');
                                        foreach ($result['tours_trip'] as $key => $value) {
                                                $result['tours_trip'][$key]['rtitle1'] = str_replace($from, $to1, $value['title']);
                                                $result['tours_trip'][$key]['rtitle'] = str_replace($from, $to, $value['title']);
                                        }
                                }
                        }
                }
                if (!empty($result['attachthumb'])) {
                        $result['images_full'] = C('imgurl') . $result['attachpath'];
                        $result['images_full_source'] = C('imgurl') . $result['attachpath'];
                        if (!empty($result['attachment'])) {
                                $result['attachment'] = $result['attachment'];
                        } else {
                                $result['attachment'] = '';
                        }
                        $result['images_list'] = explode(',', $result['attachment']);
                } else {
                        $result['images_full'] = "/public/tours/images/nophoto.png";
                        $result['images_full_source'] = "/public/tours/images/nophoto1.png";
                }
                if (!empty($result['type'])) {
                        $result['type_name'] = M("ToursType")->where(array("id" => array("eq", $result['type'])))->getField("name");
                }
                $result['delay_time'] = array();
                $this_year = date("Y");
                $this_month = date("m");
                $this_day = date("d");
                $tn = 1;
                for ($i = 1; $i <= 160; $i++) {
                        if ($tn > 30)
                                break;
                        $show_day = date("Y-m-d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                        $show_week = date("w", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                        $show_tian = date("d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                        $show_week_ch = $this->show_week_ch[$show_week];
                        $show_day_time = strtotime($show_day);

                        if (!empty($result['tours_price'])) {
                                foreach ($result['tours_price'] as $key => $value) {
                                        if ($value['case_id'] == '0' && $value['sprice'] == $result['price']) {
                                                $leixin = $value['leixin'];
                                                break;
                                        }
                                }
                                foreach ($result['tours_price'] as $key => $value) {
                                        //显示标签相同的第一个标签下的价格汇总
                                        if ($value['leixin'] == $leixin) {
                                                $value['price_value'] = unserialize($value['price_value']);
                                                switch ($value['price_type']) {
                                                        case 1:
                                                                if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                        if ($value['price_value']['adult'] > 0) {
                                                                                $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'] . "元/人";

                                                                                if ($value['price_value']['child'] > 0) {
                                                                                        $result['delay_time'][$show_day] .= " " . $value['price_value']['child'] . "元/儿童";
                                                                                }

                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                        case 2:
                                                                if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                        $value['price_value'] = array_values($value['price_value']);
                                                                        $adult_index = ($show_week - 1) < 0 ? 6 : ($show_week - 1);
                                                                        $child_index = ($show_week + 13 ) < 14 ? 20 : ($show_week + 13);
                                                                        if ($value['price_value'][$adult_index] > 0) {
                                                                                $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value'][$adult_index] . "元/人";
                                                                                if ($value['price_value'][$child_index] > 0) {
                                                                                        $result['delay_time'][$show_day] .= " " . $value['price_value'][$child_index] . "元/儿童";
                                                                                }
                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                        case 3:
                                                                if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                        foreach ($value['price_value']['day'] as $k => $val) {
                                                                                if ($val == $show_tian && $value['price_value']['adult'][$k] > 0) {
                                                                                        $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'][$k] . "元/人";
                                                                                        if ($value['price_value']['child'][$k] > 0) {
                                                                                                $result['delay_time'][$show_day] .= " " . $value['price_value']['child'][$k] . "元/儿童";
                                                                                        }
                                                                                        $tn++;
                                                                                        break 3;
                                                                                }
                                                                        }
                                                                }
                                                                break;
                                                        case 4:
                                                                if ($show_day_time == strtotime($value['start_time'])) {
                                                                        if ($value['price_value']['adult'] > 0) {
                                                                                $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'] . "元/人";
                                                                                if ($value['price_value']['child'] > 0) {
                                                                                        $result['delay_time'][$show_day] .= " " . $value['price_value']['child'] . "元/儿童";
                                                                                }
                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                }
                                        }
                                }
                        }
                }
        }

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                $this_year = date("Y");
                $this_month = date("m");
                $this_day = date("d");
                foreach ($resultSet as $key => $value) {
                        $resultSet[$key]['url'] = U('/xianlu/' . $value['id'], '', 'html', false, true);
                        $resultSet[$key]['type_name'] = M("ToursType")->field(true)->where(array('id' => $value['type']))->cache()->getField("name");
                        $resultSet[$key]['tag_name'] = M("ToursTag")->field(true)->where(array('id' => $value['tag']))->cache()->getField("name");
                        $resultSet[$key]['leavetimeinfo'] = '';
                        $resultSet[$key]['leavetimeinfo_full'] = '';
                        if (empty($value['leave_time']) && !empty($value['tours_price'])) {
                                $tn = 1;
                                for ($i = 1; $i <= 160; $i++) {
                                        $show_day = date("Y-m-d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                        $show_week = date("w", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                        $show_tian = date("d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                        $show_day_time = strtotime($show_day);
                                        foreach ($value['tours_price'] as $v) {
                                                $v['price_value'] = unserialize($v['price_value']);
                                                switch ($v['price_type']) {
                                                        case 1:
                                                                if ($show_day_time >= strtotime($v['start_time']) && ($show_day_time <= strtotime($v['end_time']) || empty($v['start_time']))) {
                                                                        if ($v['price_value']['adult'] > 0) {
                                                                                if ($tn <= 2) {
                                                                                        $resultSet[$key]['leavetimeinfo'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                }
                                                                                $resultSet[$key]['leavetimeinfo_full'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                        case 2:
                                                                if ($show_day_time >= strtotime($v['start_time']) && ($show_day_time <= strtotime($v['end_time']) || empty($v['start_time']))) {
                                                                        $v['price_value'] = array_values($v['price_value']);
                                                                        $adult_index = ($show_week - 1) < 0 ? 6 : ($show_week - 1);
                                                                        if ($v['price_value'][$adult_index] > 0) {
                                                                                if ($tn <= 2) {
                                                                                        $resultSet[$key]['leavetimeinfo'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                }
                                                                                $resultSet[$key]['leavetimeinfo_full'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                        case 3:
                                                                if ($show_day_time >= strtotime($v['start_time']) && ($show_day_time <= strtotime($v['end_time']) || empty($v['start_time']))) {
                                                                        foreach ($v['price_value']['day'] as $k => $val) {
                                                                                if ($val == $show_tian && $v['price_value']['adult'][$k] > 0) {
                                                                                        if ($tn <= 2) {
                                                                                                $resultSet[$key]['leavetimeinfo'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                        }
                                                                                        $resultSet[$key]['leavetimeinfo_full'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                        $tn++;
                                                                                        break 3;
                                                                                }
                                                                        }
                                                                }
                                                                break;
                                                        case 4:
                                                                if ($show_day_time == strtotime($v['start_time'])) {
                                                                        if ($v['price_value']['adult'] > 0) {
                                                                                if ($tn <= 2) {
                                                                                        $resultSet[$key]['leavetimeinfo'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                }
                                                                                $resultSet[$key]['leavetimeinfo_full'] .= ", " . date("m", $show_day_time) . "/" . date("d", $show_day_time);
                                                                                $tn++;
                                                                                break 2;
                                                                        }
                                                                }
                                                                break;
                                                }
                                        }
                                }
                                if (empty($resultSet[$key]['leavetimeinfo'])) {
                                        $resultSet[$key]['leavetimeinfo'] = "固定日期发团";
                                        $resultSet[$key]['leavetimeinfo_full'] = "固定日期发团";
                                } else {
                                        $resultSet[$key]['leavetimeinfo'] = substr($resultSet[$key]['leavetimeinfo'], 1);
                                        $resultSet[$key]['leavetimeinfo_full'] = substr($resultSet[$key]['leavetimeinfo_full'], 1);
                                }
                        } else {
                                $resultSet[$key]['leavetimeinfo'] = $value['leave_time'];
                                $resultSet[$key]['leavetimeinfo_full'] = $value['leave_time'];
                        }
                        if ($value['attachthumb']) {
                                $resultSet[$key]['images_full'] = C('imgurl') . $value['attachpath'];
                                $resultSet[$key]['images_full_source'] = C('imgurl') . $value['attachpath'];
                        } else {
                                $resultSet[$key]['images_full'] = "/public/tours/images/nophoto.png";
                                $resultSet[$key]['images_full_source'] = "/public/tours/images/nophoto1.png";
                        }
                }
        }

        public function getPriceByDay($ordertime, $price_id, $data = "") {
                if (empty($data))
                        $data = $this->data;
                if (empty($data))
                        return null;
                $show_day_time = strtotime($ordertime);
                $show_week = date("w", $show_day_time);
                $show_tian = date("d", $show_day_time);

                $price = array();
                foreach ($data['tours_price'] as $value) {
                        $value['price_value'] = unserialize($value['price_value']);
                        if ($price_id != '' && $price_id !== '') {
                                if ($value['id'] == $price_id) {
                                        switch ($value['price_type']) {
                                                case 1:
                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                if ($value['price_value']['adult'] > 0 && empty($price[$value['case_id']]['adult'])) {
                                                                        $price[$value['case_id']]['adult'] = $value['price_value']['adult'];
                                                                        $price[0]['adult'] = $value['price_value']['adult'];
                                                                        if ($value['price_value']['child'] > 0) {
                                                                                $price[$value['case_id']]['child'] = $value['price_value']['child'];
                                                                                $price[0]['child'] = $value['price_value']['child'];
                                                                        } else {
                                                                                $price[$value['case_id']]['child'] = $value['price_value']['adult'];
                                                                                $price[0]['child'] = $value['price_value']['adult'];
                                                                        }
                                                                }
                                                        }
                                                        break;
                                                case 2:
                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                $value['price_value'] = array_values($value['price_value']);
                                                                $adult_index = ($show_week - 1) < 0 ? 6 : ($show_week - 1);
                                                                $child_index = ($show_week + 13 ) < 14 ? 20 : ($show_week + 13);
                                                                if ($value['price_value'][$adult_index] > 0 && empty($price[$value['case_id']]['adult'])) {
                                                                        $price[$value['case_id']]['adult'] = $value['price_value'][$adult_index];
                                                                        $price[0]['adult'] = $value['price_value'][$adult_index];
                                                                        if ($value['price_value'][$child_index] > 0) {
                                                                                $price[$value['case_id']]['child'] = $value['price_value'][$child_index];
                                                                                $price[0]['child'] = $value['price_value'][$child_index];
                                                                        } else {
                                                                                $price[$value['case_id']]['child'] = $value['price_value'][$adult_index];
                                                                                $price[0]['child'] = $value['price_value'][$adult_index];
                                                                        }
                                                                }
                                                        }
                                                        break;
                                                case 3:
                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                foreach ($value['price_value']['day'] as $k => $val) {
                                                                        if ($val == $show_tian && $value['price_value']['adult'][$k] > 0 && empty($price[$value['case_id']]['adult'])) {
                                                                                $price[$value['case_id']]['adult'] = $value['price_value']['adult'][$k];
                                                                                $price[0]['adult'] = $value['price_value']['adult'][$k];
                                                                                if ($value['price_value']['child'][$k] > 0) {
                                                                                        $price[$value['case_id']]['child'] = $value['price_value']['child'][$k];
                                                                                        $price[0]['child'] = $value['price_value']['child'][$k];
                                                                                } else {
                                                                                        $price[$value['case_id']]['child'] = $value['price_value']['adult'][$k];
                                                                                        $price[0]['child'] = $value['price_value']['adult'][$k];
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        break;
                                                case 4:
                                                        if ($show_day_time == strtotime($value['start_time'])) {
                                                                if ($value['price_value']['adult'] > 0 && empty($price[$value['case_id']]['adult'])) {
                                                                        $price[$value['case_id']]['adult'] = $value['price_value']['adult'];
                                                                        $price[0]['adult'] = $value['price_value']['adult'];
                                                                        if ($value['price_value']['child'] > 0) {
                                                                                $price[$value['case_id']]['child'] = $value['price_value']['child'];
                                                                                $price[0]['child'] = $value['price_value']['child'];
                                                                        } else {
                                                                                $price[$value['case_id']]['child'] = $value['price_value']['adult'];
                                                                                $price[0]['child'] = $value['price_value']['adult'];
                                                                        }
                                                                }
                                                        }
                                                        break;
                                        }
                                }
                        }
                }
                return $price;
        }

}
